<template>
  <t-modal
    title="详情"
    width="900px"
    :visible="visible"
    @cancel="closeModal()"
    wrapClassName="add-annotations-container"
    v-bind="footer"
    forceRender
  >
    <a-form-model ref="ruleForm" :model="form" :rules="rules">
      <a-form-model-item label="图纸编号">
        <a-input v-model="form.cadCode" disabled></a-input>
      </a-form-model-item>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="问题名称">
            <a-input
              v-model="form.issueName"
              disabled
              :title="form.issueName"
            ></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="问题类型">
            <a-select style="width: 100%" disabled v-model="form.issueType">
              <a-select-option
                :value="item.dicItemCode"
                v-for="item in issueTypeSoucre"
                :key="item.dicItemCode"
              >
                {{ item.dicItemName }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :span="12">
          <a-form-model-item label="问题等级">
            <a-select style="width: 100%" disabled v-model="form.issueGrade">
              <a-select-option
                :value="item.dicItemCode"
                v-for="item in issueGradeSoucre"
                :key="item.dicItemCode"
              >
                {{ item.dicItemName }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="期望修改时间">
            <a-input v-model="form.solveTime" disabled></a-input>
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-form-model-item label="问题描述">
        <a-input
          type="textarea"
          :rows="4"
          v-model="form.issueDesc"
          disabled
        ></a-input>
      </a-form-model-item>
      <a-form-model-item
        label="图片"
        prop="files"
        :rules="{
          required: false,
          message: '请选择图片',
        }"
      >
        <div class="upload-img">
          <div class="upload-button" @click="addAnnotationsImg()" v-if="false">
            <a-icon type="plus" />
            <div class="ant-upload-text">
              上传
            </div>
          </div>
          <div
            class="file-list"
            v-for="(item, index) in form.files"
            :key="index"
          >
            <img :src="item" @click="previewImg(item)" title="点击预览" />
            <!-- 删除按钮 -->
            <div
              class="delete-button"
              @click="deleteImg(item, index)"
              v-if="false"
            >
              <a-icon
                type="close"
                :style="{
                  fontSize: '12px',
                  color: '#ffffff',
                }"
              />
            </div>
          </div>
        </div>
      </a-form-model-item>
      <a-form-model-item label="备注">
        <a-input
          type="textarea"
          :rows="4"
          v-model="form.remark"
          disabled
        ></a-input>
      </a-form-model-item>
    </a-form-model>
    <!-- 图片预览弹窗 -->
    <previewImage
      :imgSrc="imgPreview"
      :visible.sync="imgVisible"
    ></previewImage>
  </t-modal>
</template>

<script>
import { PersonnelPopup } from "@/components";
import previewImage from "@/components/TopVUI/custom/previewImage";
import { detailAnnotations } from "@/api/design";
import { getDicItemByCode } from "@/api/common.js";
export default {
  inject: ["drawingModel"],
  components: {
    PersonnelPopup,
    previewImage,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    reOpen: {
      type: Boolean,
      default: false,
    },
    operateType: {
      type: [String, Number],
      default: "1",
    },
    parameter: {
      type: Object,
      default() {
        return {};
      },
    },
    footer: {
      type: Object,
      default() {
        return {
          footer: null,
        };
      },
    },
    snapshot: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      //问题类型
      issueTypeSoucre: [],
      //问题等级
      issueGradeSoucre: [],
      submitLoading: false,
      form: {
        uuid: "",
        //最外层数据id
        drawingReviewUuid: "",
        //图纸id
        cadFileUuid: "",
        //图纸编号
        cadCode: "",
        //问题名称
        issueName: "",
        //问题类型
        issueType: "",
        //问题等级
        issueGrade: "",
        //期望修改时间
        solveTime: "",
        //问题描述
        issueDesc: "",
        //图片
        files: [],
        //备注
        remark: "",
      },
      rules: {
        cadCode: [
          {
            required: true,
            message: "请输入图纸编号",
          },
        ],
        issueName: [
          {
            required: true,
            message: "请选择问题名称",
          },
        ],
      },
      //图片预览弹窗
      imgVisible: false,
      imgPreview: "",
    };
  },

  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.getData();
          this.getDetail();
        }
      },
      immediate: true,
    },
  },

  methods: {
    async getData() {
      //问题类型
      this.issueTypeSoucre = await this.getDicCodeData("issueTypeSJ");
      //问题等级
      this.issueGradeSoucre = await this.getDicCodeData("issueGrade");
      await this.getDetail();
    },

    //查询字典表
    getDicCodeData(dicCode = "") {
      return new Promise((resolve, reject) => {
        getDicItemByCode({ code: dicCode })
          .then((res) => {
            let { data } = res;
            resolve(data);
          })
          .catch((_) => {
            reject();
          })
          .finally((_) => {});
      });
    },

    //详情
    getDetail() {
      let { uuid } = this.parameter;
      detailAnnotations({ uuid })
        .then((res) => {
          let { data } = res;
          this.form = {
            //批注id
            uuid: data.uuid,
            //最外层数据id
            drawingReviewUuid: data.drawingReviewUuid,
            //图纸id
            cadFileUuid: data.cadFileUuid,
            //图纸编号
            cadCode: data.cadCode,
            //问题名称
            issueName: data.issueName,
            //问题类型
            issueType: data?.issueType ?? undefined,
            //问题等级
            issueGrade: data?.issueGrade ?? undefined,
            //期望修改时间
            solveTime: data?.solveTime,
            //问题描述
            issueDesc: data.issueDesc,
            //图片
            files: data.dealImages,
            //备注
            remark: data.remark,
          };
        })
        .catch((_) => {});
    },

    //预览
    previewImg(data) {
      this.imgPreview = data;
      this.imgVisible = true;
    },

    //重置表单
    resetForm() {
      this.form = {
        uuid: "",
        //最外层数据id
        drawingReviewUuid: "",
        //图纸id
        cadFileUuid: "",
        //图纸编号
        cadCode: "",
        //问题名称
        issueName: "",
        //问题类型
        issueType: "",
        //问题等级
        issueGrade: "",
        //期望修改时间
        solveTime: "",
        //问题描述
        issueDesc: "",
        //图片
        files: [],
        //备注
        remark: "",
      };
      this.$refs.ruleForm.resetFields();
    },

    //关闭
    closeModal() {
      this.resetForm();
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.add-annotations-container {
  .upload-img {
    display: flex;
    flex-wrap: wrap;
    .upload-button {
      display: flex;
      width: 110px;
      height: 110px;
      border-radius: 6px;
      background: #fafafa;
      border: 1px dashed #d9d9d9;
      cursor: pointer;
      align-items: center;
      justify-content: center;
      margin-right: 8px;
      flex-direction: column;
      .anticon-plus {
        font-size: 26px;
      }
    }
    .file-list {
      width: 110px;
      height: 110px;
      background: #fafafa;
      border-radius: 6px 6px 6px 6px;
      border: 1px solid #d9d9d9;
      margin: 0 8px 8px 0;
      position: relative;
      img {
        cursor: pointer;
        height: 100%;
        width: 100%;
        object-fit: contain;
        border-radius: 6px;
      }
      .delete-button {
        position: absolute;
        width: 24px;
        height: 20px;
        background: #ff2d2d;
        border-radius: 0px 6px 0px 20px;
        opacity: 0.6;
        top: 0;
        right: 0;
        cursor: pointer;
        z-index: 99;
        .anticon-close {
          position: relative;
          top: -10px;
          right: -8px;
          font-size: 12px;
          color: #ffffff;
        }
      }
    }
  }
  .ant-form-item {
    display: flex;
    .ant-form-item-label {
      flex-shrink: 0;
      width: 100px;
    }
    .ant-form-item-control-wrapper {
      flex: 1;
      overflow: hidden;
    }
  }
}
</style>
